package com.API.dal.providers;

import com.API.dal.entities.EmployeeProject;
import com.API.dal.entities.Employees;
import com.API.dal.entities.EmployeesCosts;
import com.API.dal.entities.Posts;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class EmployeesProviderImp implements EmployeesProvider{

    @Autowired
    SessionFactory sessionFactory;

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addEmployee(Employees employee) {
        Session session = sessionFactory.getCurrentSession();
        session.save(employee);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addEmployeeProjectWork(EmployeeProject employeeProject) {
        Session session =sessionFactory.getCurrentSession();
        session.save(employeeProject);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addEmployeeCost(EmployeesCosts employeesCosts) {
        Session session =sessionFactory.getCurrentSession();
        session.save(employeesCosts);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateEmployee(Employees employee) {
        Session session = sessionFactory.getCurrentSession();
        session.update(employee);
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<EmployeeProject> getEmployeesProjects(int project_id) {
        List<EmployeeProject> list;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from EmployeeProject ep where ep.objects.id = " + project_id);
        list = query.list();
        return list;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<EmployeeProject> getEmployeeProjects(String employee_name) {
        List<EmployeeProject> list;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from EmployeeProject ep where ep.employees.name = '" + employee_name + "'");
        list = query.list();
        return list;

    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Employees> getEmployees() {
        List<Employees> employeesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Employees em");
        employeesList = query.list();
        return employeesList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Employees> getProjectEmployees(int project_id) {
        List<Employees> employeesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("select em from Employees em, EmployeeProject ep " +
                "where em.id = ep.employees.id and ep.objects.id =  " + project_id + " group by em.id");
        employeesList = query.list();
        return employeesList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Employees> getEmployeeByPriceType(String salary_type) {
        List<Employees> employeesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Employees em, SalaryType st where em.salaryType.name = " + salary_type);
        employeesList = query.list();
        return employeesList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public Employees getEmployeeById(int id) {
        Employees employee;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Employees em where em.id = " + id);
        employee = (Employees) query.uniqueResult();
        return employee;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Employees> getEmployeesByName(String employeeName) {
        List<Employees> employeesList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Employees em where em.name = '" + employeeName + "'");
        employeesList = query.list();
        return employeesList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<EmployeeProject> getEmployeeHous(int employee_id) {
        List<EmployeeProject> list;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from EmployeeProject ep where ep.employees.id = " + employee_id);
        list = query.list();
        return list;

    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Posts> getPosts() {
        List<Posts> postsList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Posts pst");
        postsList = query.list();
        return postsList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public Posts getPostByName(String name) {
        Posts post;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Posts ps where ps.name = '" + name +"'");
        post = (Posts) query.uniqueResult();
        return post;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public Posts getPostById(int id) {
        Posts post;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Posts ps where ps.id = " + id );
        post = (Posts) query.uniqueResult();
        return post;
    }
}
